class AudioManager {

    /**
     * 是否静音
     * @protected
     */
    protected isMute = false
    /**
     * 按钮点击的水滴声音
     * @protected
     */
    protected diAudio: cc.AudioClip

    /**
     * 背景音乐资源
     * @protected
     */
    protected bgAudio: cc.AudioClip

    constructor() {
        // cc.resources.preload('di', cc.AudioClip)
        // cc.resources.preload('bg', cc.AudioClip)
        // this.playBgAudio()
    }

    /**
     * 播放背景音乐
     */
    async playBgAudio() {
        if (this.getIsMute()) {
            return
        }
        try {
            const clipAudio = await this.getBgAudio();
            cc.audioEngine.playMusic(clipAudio, true);
        } catch (e) {

        }
    }

    /**
     * 设置静音状态
     * @param val
     */
    setIsMute(val: boolean = true) {
        this.isMute = val
        if (val) {
            this.stopAllAudio()
        }
    }

    stopAllAudio() {
        cc.audioEngine.stopMusic();
        cc.audioEngine.pauseAllEffects();
    }

    /**
     * 获取静音状态
     */
    getIsMute() {
        return this.isMute
    }

    /**
     * 按钮点击的水滴声音
     */
    async getDiAudio() {
        if (this.diAudio) {
            return this.diAudio;
        }
        return new Promise<cc.AudioClip>(resolve => {
            cc.resources.load('di', cc.AudioClip, (err, clip: cc.AudioClip) => {
                this.diAudio = clip
                resolve(clip)
            });
        })

    }

    /**
     * 背景音乐
     */
    async getBgAudio() {
        if (this.bgAudio) {
            return this.bgAudio;
        }
        return new Promise<cc.AudioClip>(resolve => {
            cc.resources.load('bg', cc.AudioClip, (err, clip: cc.AudioClip) => {
                this.bgAudio = clip
                resolve(clip)
            });
        })

    }

    /**
     * 播放按钮点击声音
     * @protected
     */
    async playBtnClickAudio() {
        if (this.getIsMute()) {
            return
        }
        try {
            const clipAudio = await this.getDiAudio();
            cc.audioEngine.playEffect(clipAudio, false);
        } catch (e) {

        }

    }
}

const audioManager = new AudioManager()
export default audioManager
